#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h> 
#include <unistd.h>

static int alarm_fired = 0;

void ding(int sig)
{
	alarm_fired = 1;
}

//告诉子进程，在5S后发送一个SIGALRM信号给它的父进程
int main()
{
	pid_t pid;

	printf("alarm application starting!\n");

	pid = fork();
	switch(pid)
	{
	case -1:
		perror("fork failed");
		exit(1);
	case 0:
		sleep(5);
		kill(getppid(), SIGALRM);
		//exit(0);
		break;
	default:
		//kill(pid, SIGALRM);
		break;
	}
//父进程通过一个调用安排好捕获SIGALARM喜好的工作，然后等待它的到来
	printf("waiting for alarm to  go off! %d\n",SIGALRM);
	(void) signal(SIGALRM,ding);

	pause();//把执行挂起，直到出现一个信号为止
	if(alarm_fired)
		printf("Ding!\n");
	printf("done!\n");
	exit(0);
}
